// 统一响应格式中间件
const responseMiddleware = (req, res, next) => {
  // 成功响应
  res.success = (data = null, message = 'success') => {
    res.json({
      code: 200,
      message,
      data,
      timestamp: Date.now()
    });
  };

  // 错误响应
  res.error = (message = '服务器错误', code = 500, data = null) => {
    res.status(code).json({
      code,
      message,
      data,
      timestamp: Date.now()
    });
  };

  // 分页响应
  res.paginate = (list = [], total = 0, page = 1, limit = 20, message = 'success') => {
    res.json({
      code: 200,
      message,
      data: {
        list,
        total,
        page: parseInt(page),
        limit: parseInt(limit),
        totalPages: Math.ceil(total / limit)
      },
      timestamp: Date.now()
    });
  };

  next();
};

module.exports = responseMiddleware; 